package test.demo;

import cn.hutool.core.util.ArrayUtil;
import tech.waterism.modelbase.PredictBase;
import tech.waterism.modelbase.PredictResult;
import com.alibaba.fastjson.JSON;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 流域汇流单位线（河道演进单位线）
 *
 * @author 初京刚
 * @version 1.0
 */

public class UH_V extends PredictBase {

    /**
     * 单位线
     */
    private double[] unitLine;

    /**
     * 序列长度
     */
    private Integer floodRange;

    /**
     * 上游河段流量
     */
    private double[] Q;

    public UH_V(Map<String, String> data) {
        super();
        Q = JSON.parseObject(data.get("Q"), double[].class);
        unitLine = JSON.parseObject(data.get("unitLine"), double[].class);      // line X + 2
        floodRange = Q.length;
    }


    /**
     * 模型计算
     */
    public Map<String, double[]> UHBmodel() {

        // 单位线
        double[] QSim = UnitLine();

        Map<String, double[]> simulationResult = new HashMap<>();
        simulationResult.put("QSim", QSim);
        return simulationResult;
    }

    /**
     * 单位线
     *
     * @return
     */
    private double[] UnitLine() {
        double[] QSim = new double[floodRange];

        for (int i = 0; i < floodRange; i++) {
            for (int j = 0; j < unitLine.length; j++) {
                if (i + j < floodRange) {
                    QSim[j + i] = QSim[j + i] + Q[i] * unitLine[j];
                }
            }
        }
        return QSim;
    }

    @Override
    public void init() {

    }

    @Override
    public PredictResult predict() {
        PredictResult result = new PredictResult();
        Map<String, double[]> map = UHBmodel();

        result.setQSim(map.get("QSim"));
        return result;
    }
}